﻿namespace Matriculex.UI.Helpers
{
    using System;
    using System.Web.Mvc;

    /// <summary>
    /// Class to check the current action on controller
    /// </summary>
    public static class IsCurrentHelper
    {
        /// <summary>
        /// Checks if the current action is what expected.
        /// </summary>
        /// <param name="helper">HtmlHelper class</param>
        /// <param name="actionName">the action name</param>
        /// <param name="controllerName">the controller name</param>
        /// <returns>True if the current action is the actionName from specified controller</returns>
        public static bool IsCurrentAction(this HtmlHelper helper, string actionName, string controllerName)
        {
            var currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
            var currentActionName = (string)helper.ViewContext.RouteData.Values["action"];

            return currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) 
                && currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase);
        }

        /// <summary>
        /// Checks the current controller
        /// </summary>
        /// <param name="helper">HtmlHelper class</param>
        /// <param name="controllerName">the controller name</param>
        /// <returns>True if the current controller is controllerName</returns>
        public static bool IsCurrentController(this HtmlHelper helper, string controllerName)
        {
            var currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];

            return currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase);
        }

        public static bool IsCurrentController(this HtmlHelper helper, params string[] controllersName)
        {
            var currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
            foreach (string controllerName in controllersName)
            {
                if (!controllerName.Equals(currentControllerName, StringComparison.InvariantCultureIgnoreCase))
                    continue;

                return true;
            }
            return false;
        }
    }
}
